<?php
/**
 * 收藏关注API控制器（统一收藏关注功能）
 * @author 王勇程 <laughskydragon@qq.com>
 * 
 * 统一管理：商品关注、取消关注、关注状态检查、关注列表、价格提醒等功能
 */

namespace app\api\controller;

use app\api\controller\ApiBase;

class Favorite extends ApiBase
{
    /**
     * 关注商品
     */
    public function followProduct()
    {
        $data = $this->filterApiParams(['user_token', 'product_id']);
        
        return $this->apiReturn($this->logicFavorite->followProduct($data));
    }

    /**
     * 取消关注商品
     */
    public function unfollowProduct()
    {
        $data = $this->filterApiParams(['user_token', 'product_id']);
        
        return $this->apiReturn($this->logicFavorite->unfollowProduct($data));
    }

    /**
     * 检查商品关注状态
     */
    public function checkFollowStatus()
    {
        $data = $this->filterApiParams(['user_token', 'product_id']);
        
        return $this->apiReturn($this->logicFavorite->checkFollowStatus($data));
    }

    /**
     * 获取关注列表
     */
    public function getFollowList()
    {
        $data = $this->filterApiParams(['user_token', 'page', 'limit', 'category']);
        
        return $this->apiReturn($this->logicFavorite->getFollowList($data));
    }

    /**
     * 切换收藏状态
     */
    public function toggleFavorite()
    {
        $data = $this->filterApiParams(['user_token', 'product_id']);
        
        return $this->apiReturn($this->logicFavorite->toggleFavorite($data));
    }

    /**
     * 检查收藏状态
     */
    public function checkFavorite()
    {
        $data = $this->filterApiParams(['user_token', 'product_id']);
        
        return $this->apiReturn($this->logicFavorite->checkFollowStatus($data));
    }

    /**
     * 设置价格提醒
     */
    public function setPriceAlert()
    {
        $data = $this->filterApiParams(['user_token', 'product_id', 'price']);
        
        return $this->apiReturn($this->logicFavorite->setPriceAlert($data));
    }

    /**
     * 获取价格提醒列表
     */
    public function getPriceAlertList()
    {
        $data = $this->filterApiParams(['user_token', 'page', 'limit']);
        
        return $this->apiReturn($this->logicFavorite->getPriceAlertList($data));
    }

    /**
     * 删除价格提醒
     */
    public function deletePriceAlert()
    {
        $data = $this->filterApiParams(['user_token', 'alert_id']);
        
        return $this->apiReturn($this->logicFavorite->deletePriceAlert($data));
    }

    /**
     * 过滤API参数
     */
    private function filterApiParams($fields = [])
    {
        $data = [];
        foreach ($fields as $field) {
            $value = $this->request->param($field);
            if ($value !== null) {
                $data[$field] = $value;
            }
        }
        return $data;
    }
}